import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:loading_indicator/loading_indicator.dart';
import 'package:path_provider/path_provider.dart';

class DialogUtil {
  static late BuildContext _context;

  static Future<void> showLoadingDialog(BuildContext context) async {
    _context = context;
     await showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) {
        return AlertDialog(
          // 背景透明
          backgroundColor: Colors.transparent,
          content: Container(
            // 背景透明
            color: Colors.transparent,
            width: 200,
            height: 100,
            child: Column(
              children: [
                Container(
                  width: 50,
                  height: 50,
                  child: LoadingIndicator(
                    indicatorType: Indicator.values[21],
                    strokeWidth: 1,
                  ),
                ),
                SizedBox(height: 16),
                Text('device_connect_tip'.tr()),
              ],
            ),
          ),
        );
      },
    );
  }

  static Future<void> hideLoadingDialog() async {
    if (!_context.mounted) return;
    if(Navigator.canPop(_context)){
      Navigator.of(_context).pop();

    }
  }
}
class BitUtils{
  static bool getBitIsOne(int value,int index){
    return( (value >> (index-1)) & 1)==1;
  }

}

class LogUtils{
  static void log(String msg) async {
    final Directory? downloadsDir =  await getDownloadsDirectory();
    File file = File('${downloadsDir?.path}/hsyb.log');
    if (!file.existsSync()) {
      file.createSync(recursive: true);
    }
    file.writeAsStringSync(msg+"\n",mode: FileMode.append);
  }
}
